<template>
  <div class="list-box">
    <task-info-list
        v-if="$route.path === '/worker/plaza/invited'"
        :loading="loading"
        :data="taskList"
        :data-items="['taskname','userId', 'type', 'deviceRequirement', 'need_num', 'deadline', 'reportPrice']">
      <template slot="option" scope="{task}">
        <el-button v-if="task.need_num!==0" type="primary" @click="gotoDetail(task.id)">参与测试</el-button>
        <el-button v-else type="info" disabled>人员已满</el-button>
      </template>
    </task-info-list>

    <task-info-list
        v-else
        :loading="loading"
        :data="taskList"
        :data-items="['taskname','username', 'type', 'deviceRequirement', 'need_num', 'deadline', 'reportPrice']">
      <template slot="option" scope="{task}">
        <el-button v-if="task.need_num!==0" type="primary" @click="gotoDetail(task)">参与测试</el-button>
        <el-button v-else type="info" disabled>人员已满</el-button>
      </template>
    </task-info-list>
  </div>
</template>

<script>
import {getAllValidTasks, getInvitedTaskByWorkerID} from "@/api/task";
import TaskInfoList from "@/components/new/info-list/TaskInfoList";
import {transformTaskType} from "@/utils/TaskType";

export default {
  name: "TaskList",
  components: {TaskInfoList},
  created() {
    this.getTaskList();
  },

  data() {
    return {
      taskList: [],
      loading: true
    }
  },

  methods: {
    async getTaskList() {
      let res;
      if (this.$route.path === '/worker/plaza/all')
        res = await getAllValidTasks(sessionStorage.getItem('userid'));
      else if (this.$route.path === '/worker/plaza/invited')
        res = await getInvitedTaskByWorkerID(sessionStorage.getItem('userid'));

      if (res.code === 500) {
        this.loading = false;
        return this.$message.error('获取任务列表失败');
      }
      if (res.code === 404) {
        this.loading = false;
        return this.$message.info('没有正在执行的任务');
      }

      this.taskList = transformTaskType(res.data);

      this.loading = false;
    },

    gotoDetail(task) {
      if(task.type === '协作测试'){
        this.$router.push(`/worker/plaza/collaborationTask/detail/${task.id}`);
      }else{
        this.$router.push(`/worker/plaza/detail/${task.id}`);
      }

    }
  }
}
</script>

<style scoped>

</style>
